/**
 * PicUtils.java  [V 1.0.0]
 * classpath : com.zyw.utils.PicUtils
 * zhangyw Create at 2014年8月11日 下午3:48:00
 */
package com.zyw.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;

/**
 * 尝试我们是否能够提取出一些图片处理的通用方法 
 * 
 * @author zhangyw create at 2014年8月11日 下午3:48:00
 */
public class PicUtils {
  private static final String TAG = "PicUtils";

  /**
   * 返回正确大小的bitmap
   * 
   * @author zhangyw
   * @param pathName
   * @param v
   * @param defaultWidth
   *          默认的长度，不推荐0
   * @param defaultHeight
   *          默认的高度，不推荐0
   * @return 解析好的bitmap，异常情况返回null.
   */
  public static Bitmap getRightBitmap(String pathName, View v, int defaultWidth, int defaultHeight) {
    // Get the dimensions of the View
    int targetW = v.getWidth();
    int targetH = v.getHeight();
    if (0 == targetH || 0 == targetW) {
      targetH = defaultHeight;
      targetW = defaultWidth;
    }
    if (0 == targetH || 0 == targetW) {
      return null;
    }
    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;
    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;
    return BitmapFactory.decodeFile(pathName, bmOptions);
  }
}
